<?php
/*
Fast Secure Contact Form
Mike Challis
http://www.642weather.com/weather/scripts.php
*/
//do not allow direct access
if ( strpos(strtolower($_SERVER['SCRIPT_NAME']),strtolower(basename(__FILE__))) ) {
 header('HTTP/1.0 403 Forbidden');
 exit('Forbidden');
}
     // Send a test email

     // new lines should be (\n for UNIX, \r\n for Windows and \r for Mac)
     //$php_eol = ( strtoupper(substr(PHP_OS,0,3) == 'WIN') ) ? "\r\n" : "\n";

	 $php_eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
	 $php_eol = (!$php_eol) ? "\n" : $php_eol;

	 $email = $_POST['si_contact_to'];
     $name = __('WordPress Contact Form Test', 'si-contact-form');
     if($this->ctf_validate_email($email)) {

		$subject = __('Test mail to ', 'si-contact-form') . $email;
		$message = __('This is a test mail generated by the Fast Secure Contact Form WordPress plugin.', 'si-contact-form').' '.$si_contact_opt['php_mailer_enable'];
        $message = wordwrap($message, 70,$php_eol);

    $smtp_debug = '';
    $ctf_email_on_this_domain = $si_contact_opt['email_from']; // optional
    // prepare the email header
    $this->si_contact_from_name = $name;
    $this->si_contact_from_email = $email;
    //$this->si_contact_mail_sender = $ctf_email_on_this_domain;
    if ($ctf_email_on_this_domain != '' ) {
         if(!preg_match("/,/", $ctf_email_on_this_domain)) {
           // just an email: user1@example.com
           $this->si_contact_mail_sender = $ctf_email_on_this_domain;
           if($email == '' || $si_contact_opt['email_from_enforced'] == 'true')
              $this->si_contact_from_email = $ctf_email_on_this_domain;
         } else {
           // name and email: webmaster,user1@example.com
           list($key, $value) = explode(",",$ctf_email_on_this_domain);
           $key   = trim($key);
           $value = trim($value);
           $this->si_contact_mail_sender = $value;
           if($name == '')
             $this->si_contact_from_name = $key;
           if($email == '' || $si_contact_opt['email_from_enforced'] == 'true')
             $this->si_contact_from_email = $value;
         }
    }
    $header_php =  "From: $this->si_contact_from_name <$this->si_contact_from_email>\n"; // header for php mail only
    $header = '';
    if ($si_contact_opt['email_reply_to'] != '') { // custom reply_to
         $header .= "Reply-To: ".$si_contact_opt['email_reply_to']."\n"; // for php mail and wp_mail
    }else if($email != '') {   // trying this: keep users reply to even when email_from_enforced
         $header .= "Reply-To: $email\n"; // for php mail and wp_mail
    }else {
         $header .= "Reply-To: $this->si_contact_from_email\n"; // for php mail and wp_mail
    }
    if ($ctf_email_on_this_domain != '') {
      $header .= "X-Sender: $this->si_contact_mail_sender\n";  // for php mail
      $header .= "Return-Path: $this->si_contact_mail_sender\n";   // for php mail
    }
    $header .= 'Content-type: text/plain; charset='. get_option('blog_charset') . $php_eol;

    @ini_set('sendmail_from', $this->si_contact_from_email);

    // Check for safe mode
    $this->safe_mode = ((boolean)@ini_get('safe_mode') === false) ? 0 : 1;

    if ($si_contact_opt['php_mailer_enable'] == 'php') {
       // sending with php mail
       $header_php .= $header;
       // Start output buffering to grab smtp debugging output
	   ob_start();
      if ($ctf_email_on_this_domain != '' && !$this->safe_mode) {
        // Pass the Return-Path via sendmail's -f command.
        $result = mail($email,$subject,$message,$header_php, '-f '.$this->si_contact_mail_sender);
      }else{
        // the fifth parameter is not allowed in safe mode
        $result = mail($email,$subject,$message,$header_php);
      }
      $smtp_debug = ob_get_clean();
    } else {
        // sending with wp_mail
        add_filter( 'wp_mail_from', array(&$this,'si_contact_form_from_email'),1);
        add_filter( 'wp_mail_from_name', array(&$this,'si_contact_form_from_name'),1);
        if ($ctf_email_on_this_domain != '') {
            // Add an action on phpmailer_init to add Sender $this->si_contact_mail_sender for Return-path in wp_mail
            // this helps spf checking when the Sender email address matches the site domain name
            add_action('phpmailer_init', array(&$this,'si_contact_form_mail_sender'),1);
        }
        global $phpmailer;
       	// Make sure the PHPMailer class has been instantiated
	    // (copied verbatim from wp-includes/pluggable.php)
	    // (Re)create it, if it's gone missing
	    if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
		    require_once ABSPATH . WPINC . '/class-phpmailer.php';
		    require_once ABSPATH . WPINC . '/class-smtp.php';
		    $phpmailer = new PHPMailer();
	    }

		// Set SMTPDebug to level 2
		$phpmailer->SMTPDebug = 2;

		// Start output buffering to grab smtp debugging output
		ob_start();

		// Send the test mail
		$result = wp_mail($email,$subject,$message,$header);

		// Grab the smtp debugging output
		$smtp_debug = ob_get_clean();
      }

		// Output the response
		?>
<div id="message" class="updated"><p><strong><?php _e('Test Message Sent', 'si-contact-form'); echo '<br />'.$si_contact_opt['php_mailer_enable']; echo ' '.$subject; ?></strong></p>
<?php if ($result != true) { ?>
<p><?php _e('The result was:', 'si-contact-form'); ?></p>
<?php echo '<p><a href="http://www.fastsecurecontactform.com/email-does-not-send">'.  __('See FAQ', 'si-contact-form') . '</a></p>'; ?>
<pre><?php esc_html(var_dump($result)); ?></pre>
<?php
if ($si_contact_opt['php_mailer_enable'] == 'wordpress') {
?>
<p><?php _e('The full debugging output is shown below:', 'si-contact-form'); ?></p>
<?php echo '<p><a href="http://www.fastsecurecontactform.com/email-does-not-send">'.  __('See FAQ', 'si-contact-form') . '</a></p>'; ?>
<pre><?php esc_html(var_dump($phpmailer)); ?></pre>
<?php
    }
} else {
  echo '<p>'._e('Be sure to check your email to see if you received it.', 'si-contact-form').'</p>';
  echo '<p><a href="http://www.fastsecurecontactform.com/email-does-not-send">'.  __('See FAQ', 'si-contact-form') . '</a></p>';
}
      if ($smtp_debug != '') {
?>
<p><?php _e('The E-mail debugging output is shown below:', 'si-contact-form'); ?></p>
<?php echo '<p><a href="http://www.fastsecurecontactform.com/email-does-not-send">'.  __('See FAQ', 'si-contact-form') . '</a></p>'; ?>
<pre><?php echo esc_html($smtp_debug) ?></pre>
<?php }

   }else{
     echo '<div id="message" class="updated"><p><strong>'.__('Test failed: Invalid E-mail address', 'si-contact-form').'</strong></p>';
   }
?>
</div>
<?php

?>